import subprocess
import sys
from typing import List


def install_packages(
    packages: List[str], mirror_url: str = "https://mirrors.aliyun.com/pypi/simple/"
) -> None:
    """使用指定镜像源安装Python包"""
    try:
        cmd = [
            "pip",
            "install",
            "--upgrade",
            "--no-cache-dir",
            f"--index-url={mirror_url}",
        ] + packages

        result = subprocess.run(cmd, check=True, text=True, capture_output=True)

        print(f"安装成功:\n{result.stdout}")

    except subprocess.CalledProcessError as e:
        print(f"安装失败: {e.stderr}")
        sys.exit(1)


if __name__ == "__main__":
    required_packages = [
        "flask",
        "flask-socketio",
        "paho-mqtt",
        "python-dotenv",
        "black",  # 添加black代码格式化工具
    ]

    print("开始安装项目依赖...")
    install_packages(required_packages)
    print("所有依赖安装完成！")
